package com.lmc.me;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.lmc.R;
import com.lmc.adapter.FaqAdapter;
import com.lmc.entity.Faq;
import com.lmc.util.JsonManager;
import com.qxzn.app.api.api.HttpRequest;
import com.qxzn.app.page.BaseActivity;
import com.qxzn.app.util.SharePreferenceUtil;

import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 常见问题
 */
public class FaqActivity extends BaseActivity {

    private RecyclerView recyclerView;
    private List<Faq.DataBean> list = new ArrayList<>();

    private FaqAdapter adapter;

    @Override
    protected int initLayout() {
        return R.layout.activity_faq;
    }

    @Override
    protected void initView() {
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    }

    @Override
    protected void iniTitleBar() {
        tv_title.setText("常见问题");
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        adapter = new FaqAdapter(mActivity, list);
        recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        recyclerView.setAdapter(adapter);
        Get();
    }

    private void Get() {
        JSONObject params = new JSONObject();

        HttpRequest.getService().getFaqList(HttpRequest.getDefaultBody(params)).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    String string = response.body().string();
                    Faq faq = JsonManager.parseJson(string, Faq.class);
                    list.clear();
                    for (Faq.DataBean dataBean : faq.getData()) {
                        list.add(dataBean);
                    }
                    adapter.notifyDataSetChanged();
                    Log.e("-----------", "查询常见问题：" + string);
                } catch (Exception e) {
                    Log.e("-----", e.toString());
                }

            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
            }
        });
    }
}
